﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is KazeOS Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon) <ekameleon@gmail.com>.
  Portions created by the Initial Developer are Copyright (C) 2004-2010
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/

package evisio.net 
{
    import evisio.ApplicationID;
    import evisio.managers.IStateManager;
    import evisio.states.StateList;
    import evisio.states.authentification.AuthentificationState;
    
    import system.events.ActionEvent;
    
    import vegas.CoreObject;
    import vegas.events.NetServerEvent;
    import vegas.ioc.factory.ECMAObjectFactory;
    import vegas.managers.ProtectorManager;
    import vegas.net.NetServerConnection;
    import vegas.net.NetServerStatus;
    
    import flash.events.NetStatusEvent;
    import flash.net.NetStream;
    
    /**
     * Invoked when the stratus NetServerConnection notify events.
     */
   public class StratusConnectionListener extends CoreObject
   {
      
        /**
         * Creates a new StratusConnectionListener instance.
         */
        public function StratusConnectionListener()
        {
            //         
        }
        
        /**
         * Indicates the NetServerConnection reference.
         */
        public var connection:NetServerConnection ;           
        
        /**
         * The IoC factory reference.
         */
        public var factory:ECMAObjectFactory ;
        
        /**
         * The locale object. This object contains all information in the localization of this instance.
         */
        public var locale:Object ;         
        
        /**
         * The protector manager reference.
         */
        public var protectorManager:ProtectorManager ;
        
        /**
         * The state manager reference.
         */
        public var stateManager:IStateManager ;        
           
        /**
         * Indicates the NetServerConnection reference.
         */
        public var stratus:NetServerConnection ;
           
        /**
         * Handles the event when the connection is accepted.
         */
        public function accept( e:NetServerEvent ):void 
        {
            logger.info( this + " accept :: " + e.info.description ) ;
            if ( factory != null )
            {
                var expert:StreamOutExpert = factory.getObject( ApplicationID.STREAM_OUT_EXPERT ) as StreamOutExpert ;
                
                logger.info( this + " stream : " + expert ) ;
                
                var stream:NetStream = expert.netStream as NetStream ;
                    
                logger.info( this + " peerStreams : [" + stream.peerStreams + "]") ;
                
                 logger.info( this + " farID     : " + stream.farID ) ;
                logger.info( this + " farNonce  : " + stream.farNonce ) ;
                logger.info( this + " nearNonce : " + stream.nearNonce ) ;
                
                var authentification:AuthentificationState = factory.getObject( StateList.AUTHENTIFICATION_STATE ) as AuthentificationState ;
                if ( authentification != null )
                {
                    authentification.reset() ;
                }
            } 
            
            if ( stateManager != null )
            {
                stateManager.currentState = StateList.ASK_STATE ;
            }
        }
        
        /**
         * Handles the event when the connection is closed.
         */
        public function close( e:NetServerEvent ):void 
        {
            logger.debug( this + " close : " + e.info ) ; 
            if ( factory != null )
            {
                factory.removeSingleton( ApplicationID.STREAM_IN_EXPERT )  ;
                factory.removeSingleton( ApplicationID.STREAM_OUT_EXPERT )  ;
            }
            closeConnection() ;
        }        
         
        /**
         * Handles the event when the connection is started.
         */
        public function finish( e:ActionEvent ):void 
        {
           logger.debug( this + " finish" ) ;
           if ( protectorManager != null && protectorManager.enabled )
           {
                protectorManager.enabled = false ;
           }
        } 
        
        /**
         * Handles the event when the connection is rejected.
         */
        public function reject( e:NetServerEvent ):void 
        {
            logger.warn( this + " reject : " + e.info ) ; 
        }
        
        /**
         * Handles the event when the connection indicates a status wich change.
         */
        public function status( e:NetStatusEvent ):void 
        {
            var code:String = e.info.code ;
            logger.debug( this + " status : " + code ) ;
            switch ( code )
            {
                case NetServerStatus.CONNECT_FAILED.code :
                {
                    closeConnection() ;
                    var authentification:AuthentificationState = factory.getObject( StateList.AUTHENTIFICATION_STATE ) as AuthentificationState ;
                    if ( authentification != null )
                    {
                        authentification.message = locale.connection_failed ;
                    }
                }
            }
        }
        
        /**
         * Handles the event when the connection is started.
         */
        public function start( e:ActionEvent ):void 
        {
           logger.debug( this + " start" ) ;
           if ( protectorManager != null )
           {
                protectorManager.enabled = true ;
           }
        }        
        
        /**
         * Close the current FMS connection of the application if is active. 
         */
        protected function closeConnection():void
        {
            if ( connection != null && connection.connected )
            {
                connection.close() ;
            }
        }
    }
}



